package com.quanyan.club.util.qrcode;

import com.quanyan.club.pullclub.utils.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;

/**
 * Created by xian-guo on 2017/3/6.
 */
public class ImageUtils {

    public static final String larkLogo="iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ\n" +
            "bWFnZVJlYWR5ccllPAAAA4ZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdp\n" +
            "bj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6\n" +
            "eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1\n" +
            "Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJo\n" +
            "dHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlw\n" +
            "dGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEu\n" +
            "MC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVz\n" +
            "b3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1N\n" +
            "Ok9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDoyMDAzMDQ4ZS1jZTExLTQyZjAtYjc3My05NTZl\n" +
            "N2Q5OTY1NWMiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6RjkyRjI1QzE3Q0EzMTFFNkIzNUNB\n" +
            "MEE0NTZBNDJCQUYiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6RjkyRjI1QzA3Q0EzMTFFNkIz\n" +
            "NUNBMEE0NTZBNDJCQUYiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTUg\n" +
            "KE1hY2ludG9zaCkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlp\n" +
            "ZDo5M2FhNTA3OC01OTUwLTQ0OTktOTU0MS1kMDk2OTBhMmQzMWUiIHN0UmVmOmRvY3VtZW50SUQ9\n" +
            "ImFkb2JlOmRvY2lkOnBob3Rvc2hvcDoyNjE4ZjQzZS1jNTAyLTExNzktYWExZS04YzM1NjI1ZWVk\n" +
            "MjMiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tl\n" +
            "dCBlbmQ9InIiPz4jrauxAAAHNklEQVR42uyd228UVRzHz1x2Zmdmd3ZLLwvSUiNIuESLaIC2NEDE\n" +
            "xAD6YgLRB01IJGh88R8Qo4mJkRgfjEJCNGAEColAQG619MYlFFSKULCAlbaSFmjp7nZ37nM82xZK\n" +
            "l15myF6mO+ebTR/mcmby6e/3Pb/zm9kscZnwAyxrIjECDAvDwrAwLAwLw8IIMCwMC8PCsDAsDAsj\n" +
            "yDQsY/tW8O6bgGVyGxaRkhaN0nDUZFmqp4c5sB/s/xXEVBxZ48s0E/EVCkmbP1L37gIb3wIii2GN\n" +
            "M0p//0hKFhZKmz5U9/wEN20AIodhPTFKz51kF8vPlzduUvbugpvfBvlCbsCiPiBSkC/krCK97KUn\n" +
            "t0OO0xctNtetoQM0uN0+1b0sNQZPzA5JO3dBmp4IaDjMHjkIqg+CuxFXRxZ4EKNKCvXn505wCPR6\n" +
            "9RcXGevW0gUc6GgHUcWlkZXQM0F123dGUcjSVQdi7LFDZPUB2NXnvshCispUe6tRWQVYCwMyjL7w\n" +
            "BeONdVRIJDv/BWHJZbCQuu7TNy+bS5YhX7dyOPR49PkLE8imB8n/boP+mGvS8NGIpQXGjz8oHA8I\n" +
            "wsZ8qsiemuPUnn3wVo+LYCEJl8/qDCv19CgMYwsZoSpM7Ulq9z54Y6RwI4qDZDAIBI5AARuPA10z\n" +
            "7tyF3dFcgXXqECwpTZSmhiH33pcJEpA2ql+EN9DZQfzVQi0tBzNLIM8nH3Dj79iaDZmHRadjUHi9\n" +
            "FQzCoihKKApxpin39UkQWkSGDuwvnkWUlIokQZvGGE2OI4en8kJ6tPTGxlHXIEm+oCAvP99LEoRh\n" +
            "WCUOYdgw+1RNU1VT10fCSpaUX3IIlnq4lrh7N/lKJCnkTQsWFHA0bQtZJBrtj0RUVTUGzyIunIfd\n" +
            "4dyBhdaAxu5dY18PRVkgkFdYyDMMgfLNMrJoNBoOh2VZ9sayVmGkq60s79hL3Gob38IJzu9Hicnz\n" +
            "PGnZ+xGyWCzWXVySrTZG2nrwii5v+RQoyoSzHioGuGAwiJChqcDiwNqMmfC9DbkFC81Z51v1z7cM\n" +
            "NVEnRRYIBARBsIhML1+ea7ASTl993PjiM2DBmxAyr9c7hGzSxDQLi3IQViIddx4U/T6Px2OtHE0g\n" +
            "E0WRnrA1RsTiuQkrcY1IxC/6UdSwLGslylAy+v3+CeKLvtqSs7DMf24mENCU4BOC0/K8nJeczMgQ\n" +
            "KRRf4+5taMhZWPK5M6PqLJ4PFOTzwiQzINrLMGM8tSX7esGJszkLK370eNKcOGznwYBvQjvjn1hC\n" +
            "IzFNDUA3cxaWerFNaxw7cVDsIDsTA6LXy44ZXGO4e01NrlXwSQp/8zV8bDGcbNg0zQvCUHVKjOp/\n" +
            "Jc8G9M02cLEtx2HJjS3Sju2T3ApFIu8P5gXRVPgwppIbh3RjHcieMvfKUd8nXylHDlkptTyMB9kZ\n" +
            "+iBfG7VL18jjta6AhVz53vsfx7//1uLhidlw9DtMnt8vwo5ed8AaXF1rzc0BUfSyLGGhN2+MbntR\n" +
            "9adAVkVn+Hr+ygpUaqGVM0oxVVVlRTHHL1A1TRv5r4bD4Ohpd8ESKqseeRM7KEREURRtrLlS10Y2\n" +
            "es40AEV1ESxyGs8ufjlpo2dQKOMQMlXT4GOLR1UboUPW1IJsK6Ow/K9WEqx3PDtHRRYHoZJgpg7l\n" +
            "pvowlDwd7eDcFXfB8lUut9KiGbIzZVDDd1lfBxygjMLiyyssHomWQQjcsPebBnHiN+gAWJkrHchQ\n" +
            "wLtosY0y42FYsX/+4ZAXIDIHS1xdCaz1S4dXSLLikPIqC7B8D4sGy7DkxP3JEjzR5DpY/NJye9X+\n" +
            "YBomulcRyV2wqNJ8pqzsKWCR2eteZQ2WuLKKICm7acgMDMDGS66D5Vtu77EoKuh1XWfPnQZOUoZg\n" +
            "Wa+wHs9BY88e18Gi50xn5i2wm4M+VYWtXa6DJa6qsvWa5FBkcVcuAYcpE7AmXRI+KU3TjX3VboRl\n" +
            "17CQvKoi1bsvsjzzij1z5tq2uatXgPOUdlji6hW2XoUfktpQ50ZYvmWVT3FWpK7RjbD4CtuGBQ1d\n" +
            "vdbpOlhs2Wz62efsnkVQdPCdtQ6Eld5OqX9l1dOdOOPLrQTjCR+rMTofuAWW3SXhSJeisHDmth0z\n" +
            "U3QbelfXtVkLnJ6G3NIKJ0SEdDY100UaYXmXzKeLi50AK3q6yemw/CuqnEAKQBg95fjI8lU4IgfV\n" +
            "tutqa6fTYXFPVY6mwbDOOL3OEl4vp0IhJ8CKNjU5Htb69Y7wK0OP1NY7HZb5ylJHGFbLpRSWtemC\n" +
            "pQrC0FPS7Cp+JpWPPNIFy4DgwYP+rMOKNDVOAVg0NOPxeH9/NnlBWRo4dXYqwLrQjP729vZ1d/dI\n" +
            "kmSaWfgCiXyh2exL5ZftiDT9hgXh5/IbTsb92fyBDPrevUjFa1MgsmBUul++imuqYwEk7LeVU3MP\n" +
            "1T+nOALwr6M4YrmDYWFYWBgWhoVhYVgYFoaFhWFhWBgWhpV7+l+AAQBnAZzw9jd43gAAAABJRU5E\n" +
            "rkJggg==";

    /**
     * 图片转换base64
     * @param imgStr
     * @param imgFilePath
     * @return
     */
    public static boolean getImageBase64(String imgStr, String imgFilePath) {
        if (imgStr == null) return false;
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out=null;
        try {
            byte[] bytes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            return true;
        } catch (Exception e) {
            return false;
        }finally {
            try {
                if (null !=out){
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }

    /**
     * base64转换图片
     * @param imageFilePath
     * @return
     */
    public static String setImageBase64(String imageFilePath){
        if(StringUtils.isBlankOrNull(imageFilePath)) return null;
        InputStream in=null;
        byte[] data = null;
        try {
            in = new FileInputStream(imageFilePath);
            data = new byte[in.available()];
            in.read(data);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally {
            if (null !=in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * 图片转换成byte
     * @param data
     * @return
     */
    public static String setImageBase64(byte[] data){
        if (data.length<=0) return null;
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * 压缩图片
     * @param sourceImage    待压缩图片
     */
    public static BufferedImage zoomImage(BufferedImage sourceImage , int rate){
        int sourceWidth=sourceImage.getWidth() * rate /100;
        int sourceHeight=sourceImage.getHeight() * rate /100;
        BufferedImage zoomImage = new BufferedImage(sourceWidth, sourceHeight, sourceImage.getType());
        Image image = sourceImage.getScaledInstance(sourceWidth, sourceHeight, Image.SCALE_SMOOTH);
        Graphics gc = zoomImage.getGraphics();
        gc.setColor(Color.WHITE);
        gc.drawImage( image , 0, 0, null);
        gc.dispose();
        image.flush();
        return zoomImage;
    }

    public static void main(String[] args) throws Exception {
//        String st=setImageBase64("G:/icon_lark_100.png");
//        System.out.println(st);
        //getImageBase64(larkLogo,"G:/BASE64.png");
//        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] b = decoder.decodeBuffer(larkLogo);
//        ByteArrayInputStream in = new ByteArrayInputStream(b);
//        BufferedImage logo = ImageIO.read(in);
//        BufferedImage bs= zoomImage(logo,50);
//        ImageIO.write(bs,"png",new File("G:/logo.png"));
    }
}
